NextJS Analytics and Add Blockers
I want to make sure I get session data from my users even if they have an add blocker. To do this I just need to create a proxy in the next.config.js
that routes my analytics data as if it's from the same domain
next.config.js
/** @type {import('next').NextConfig} */
const ANALYTICS_URL = process.env.NEXT_PUBLIC_UMAMI_URL + '/:match*'
console.log('ANALYTICS_URL', ANALYTICS_URL);
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
async rewrites() {
return [
{
source: '/stats/:match*',
destination: ANALYTICS_URL,
},
]
},
}
module.exports = nextConfig
I first tried this out on my Make-a-Gram project
Credits
- next.config.js: Rewrites | Next.js (nextjs.org)
- How to use a proxy in Next.js - LogRocket Blog
- Running on Vercel | umami